Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Nested If

Nested If - Example - 2

Here are the next set of examples for Nested if else statements to understand the concept of nested if in Java but these conditional statements are common in most of the languages,

Example 1: Quadrant Determination

Nested if Quadrant Determination
public class Main{ public static void main(String[] args) { double x = 2.5; double y = -3.8; // Check the value of x if (x > 0) { // If x is positive, check the value of y if (y > 0) { System.out.println("Point is in Quadrant I."); } else if (y < 0) { System.out.println("Point is in Quadrant IV."); } else { System.out.println("Point is on the positive x-axis."); } } else if (x < 0) { // If x is negative, check the value of y if (y > 0) { System.out.println("Point is in Quadrant II."); } else if (y < 0) { System.out.println("Point is in Quadrant III."); } else { System.out.println("Point is on the negative x-axis."); } } else { // If x is neither positive nor negative, check the value of y if (y != 0) { System.out.println("Point is on the y-axis."); } else { System.out.println("Point is at the origin."); } } } }

Output

Point is in Quadrant IV.
In this example, The code starts by declaring two double variables, x and y, with values 2.5 and -3.8 respectively. These variables represent the coordinates of a point on the Cartesian plane. The code then enters a series of nested if and else statements to determine the quadrant or axis on which the point is located: The first outer if statement checks whether the value of x is greater than 0. If x is positive, it means the point is in the right half of the plane. Inside this first if block, another if statement checks whether the value of y is greater than 0. If both x and y are positive, the program prints "Point is in Quadrant I." If y is not greater than 0 but is less than 0 (negative), the program prints "Point is in Quadrant IV." If neither of the above conditions is met, it means y is 0, and the program prints "Point is on the positive x-axis." If the value of x is not greater than 0, the program moves to the else if statement that checks whether x is less than 0. If x is negative, it means the point is in the left half of the plane. Inside this else if block, similar checks are performed on the value of y to determine whether the point is in Quadrant II (positive y, negative x), Quadrant III (negative x and y), or on the negative x-axis. If neither x is positive nor negative, it means x is 0. The program then enters the final else block to check the value of y. If y is not 0, the program prints "Point is on the y-axis." If both x and y are 0, the program prints "Point is at the origin." In summary, the code determines the location of the point based on the values of x and y and prints the corresponding message indicating the quadrant or axis on which the point is located.

Example 2: Ticket Pricing with Discount Levels

Ticket Pricing using Nested If
public class Main{ public static void main(String[] args) { boolean isStudent=true; int age=16; if (age < 18) { // The user is a child or a student. if (isStudent) { // The user is a student. System.out.println("Student discount applies."); } else { // The user is a child. System.out.println("Child ticket price applies."); } } else if (age >= 18 && age <= 30) { // The user is an adult. if (isStudent) { // The user is a student. System.out.println("Student discount applies."); } else { // The user is an adult. System.out.println("Regular adult ticket price applies."); } } else { // The user is a senior citizen. System.out.println("Senior citizen discount applies."); } } }

Output

Student discount applies.
In this example, The code first declares two variables: age and isStudent. The age variable stores the user's age, and the isStudent variable stores a boolean value that indicates whether the user is a student. The code then uses a series of nested if statements to determine the ticket price that applies to the user. The first if statement checks if the user's age is less than 18. If it is, then the second if statement is evaluated. The second if statement checks if the user is a student. If the user is a student, then the message "Student discount applies" is printed to the console. Otherwise, the message "Child ticket price applies" is printed to the console. If the user's age is not less than 18, then the first if statement is skipped. The code then checks if the user's age is between 18 and 30. If it is, then the third if statement is evaluated. The third if statement checks if the user is a student. If the user is a student, then the message "Student discount applies" is printed to the console. Otherwise, the message "Regular adult ticket price applies" is printed to the console. If the user's age is not between 18 and 30, then the code prints the message "Senior citizen discount applies" to the console.

Example 3: Character Classification

Character classification example in java public class Main{ public static void main(String[] args) { char symbol = '$'; // Check if the symbol is a letter or digit if (Character.isLetterOrDigit(symbol)) { // If the symbol is a letter, check if it's uppercase or lowercase if (Character.isLetter(symbol)) { // If the letter is uppercase, print "Uppercase letter." if (Character.isUpperCase(symbol)) { System.out.println("Uppercase letter."); } // If the letter is not uppercase, print "Lowercase letter." else { System.out.println("Lowercase letter."); } } // If the symbol is not a letter, it must be a digit, so print "Digit." else { System.out.println("Digit."); } } // If the symbol is neither a letter nor a digit, print "Special character." else { System.out.println("Special character."); } } }

Output

Special character.
In this example, The code starts by declaring a char variable named symbol and assigning it the value '$'. This represents the character that needs to be categorized. The code then enters a series of nested if and else statements to categorize the character: The first outer if statement uses Character.isLetterOrDigit(symbol) to check if the symbol is either a letter or a digit. If this condition is true, it means the character is either a letter or a digit and not a special character. Inside this first if block, another if statement checks if the symbol is a letter using Character.isLetter(symbol). If the symbol is indeed a letter, the program then checks if the letter is uppercase by using Character.isUpperCase(symbol). If the letter is uppercase, it prints "Uppercase letter." If not, it prints "Lowercase letter." If the symbol is not a letter (i.e., it's a digit), the program prints "Digit." If the condition of the first outer if statement is not met, it means the symbol is neither a letter nor a digit, so the program enters the else block and prints "Special character." In summary, the code determines the category of the input character and prints one of the following messages: If the character is an uppercase letter: "Uppercase letter." If the character is a lowercase letter: "Lowercase letter." If the character is a digit: "Digit." If the character is a special character: "Special character."

  📌TAGS

★If else condition ★java ★java if ★if else ★nested if else ★nested if ★conditional statements

Tutorials